/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
using System;
namespace Velocity4Net.Runtime.Logging
{

    /**
     * This is a wrapper around a log object, that can add a prefix to log messages
     * and also turn logging on and off dynamically. It is mainly used to control the
     * logging of VelociMacro generation messages but is actually generic enough code.
     *

     * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
     * @version $Id: LogDisplayWrapper.java 685685 2008-08-13 21:43:27Z nbubna $
     * @since 1.5
     */
    public class LogDisplayWrapper
            : Log
    {
        /** The prefix to record with every log message */
        private readonly String prefix;

        /** log messages only if true */
        private readonly bool outputMessages;

        /** The Log object we wrap */
        private readonly Log log;

        /**
         * Create a new LogDisplayWrapper
         * @param log The Log object to wrap.
         * @param prefix The prefix to record with all messages.
         * @param outputMessages True when messages should actually get logged.
         */
        public LogDisplayWrapper(Log log, String prefix, bool outputMessages)
            : base(log.LogChute)
        {
            this.log = log;
            this.prefix = prefix;
            this.outputMessages = outputMessages;
        }

        /**
         * make sure that we always use the right LogChute Object
         */
        protected LogChute LogChute
        {
            get
            {
                return log.LogChute;
            }
        }

        /**
         * @see Log#log(int, Object)
         */
        protected void Log(int level, Object message)
        {
            Log(outputMessages, level, message);
        }

        protected void Log(bool doLogging, int level, Object message)
        {
            if (doLogging)
            {
                LogChute.log(level, prefix + "" + message);
            }
        }

        /**
         * @see Log#log(int, Object, Throwable)
         */
        protected void Log(int level, Object message, System.Exception t)
        {
            Log(outputMessages, level, message);
        }

        protected void Log(bool doLogging, int level, Object message, System.Exception t)
        {
            if (doLogging)
            {
                LogChute.log(level, prefix + "" + message, t);
            }
        }

        /**
         * Log a trace message.
         * @param doLogging Log only if this parameter is true.
         * @param message
         */
        public void Trace(bool doLogging, Object message)
        {
            //Log(doLogging, LogChute.TRACE_ID, message);
        }

        /**
         * Log a trace message and accompanying Throwable.
         * @param doLogging Log only if this parameter is true.
         * @param message
         * @param t
         */
        public void Trace(bool doLogging, Object message, System.Exception t)
        {
            //Log(doLogging, LogChute.TRACE_ID, message, t);
        }

        /**
         * Log a debug message.
         * @param doLogging Log only if this parameter is true.
         * @param message
         */
        public void Debug(bool doLogging, Object message)
        {
            Log(doLogging, LogConstants.DEBUG_ID, message);
        }

        /**
         * Log a debug message and accompanying Throwable.
         * @param doLogging Log only if this parameter is true.
         * @param message
         * @param t
         */
        public void Debug(bool doLogging, Object message, System.Exception t)
        {
            Log(doLogging, LogConstants.DEBUG_ID, message, t);
        }

        /**
         * Log an info message.
         * @param doLogging Log only if this parameter is true.
         * @param message
         */
        public void Info(bool doLogging, Object message)
        {
            Log(doLogging, LogConstants.INFO_ID, message);
        }

        /**
         * Log an info message and accompanying Throwable.
         * @param doLogging Log only if this parameter is true.
         * @param message
         * @param t
         */
        public void Info(bool doLogging, Object message, System.Exception t)
        {
            Log(doLogging, LogConstants.INFO_ID, message, t);
        }

        /**
         * Log a warning message.
         * @param doLogging Log only if this parameter is true.
         * @param message
         */
        public void Warn(bool doLogging, Object message)
        {
            Log(doLogging, LogConstants.WARN_ID, message);
        }

        /**
         * Log a warning message and accompanying Throwable.
         * @param doLogging Log only if this parameter is true.
         * @param message
         * @param t
         */
        public void Warn(bool doLogging, Object message, System.Exception t)
        {
            Log(doLogging, LogConstants.WARN_ID, message, t);
        }

        /**
         * Log an error message.
         * @param doLogging Log only if this parameter is true.
         * @param message
         */
        public void Error(bool doLogging, Object message)
        {
            Log(doLogging, LogConstants.ERROR_ID, message);
        }

        /**
         * Log an error message and accompanying Throwable.
         * @param doLogging Log only if this parameter is true.
         * @param message
         * @param t
         */
        public void Error(Boolean doLogging, Object message, System.Exception t)
        {
            Log(doLogging, LogConstants.ERROR_ID, message, t);
        }
    }

}